<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/09/20 22:11:20 $)
// $RCSfile: admin_database.php,v $ : $Revision: 1.10 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if (!$staffauth->isAdmin()) {
	trigger_error("You do not have permission to access $PHP_SELF", E_USER_ERROR);
}

$template->loadLanguageSection("database");

/**
* Gets the Table List from the database as an array
*/
function getTableList()
{
	global $_SWIFT, $dbCore, $_DB;

	$tablelist = array();

	$dbCore->query("SHOW TABLE STATUS FROM `". $dbCore->escape($_DB["name"]) ."`;");
	while ($dbCore->nextRecord())
	{
		if (trim($dbCore->Record["Name"]) != "")
		{
			$tablelist[] = $dbCore->Record["Name"];
		}
	}

	return $tablelist;
}

global $_DB;

/**
* ###############################################
* Backup of Database
* ###############################################
*/
if ($eventaction == "backup")
{
	if ($_POST["step"] == 1 && $_SWIFT["isdemo"] != true)
	{
		require_once ("./includes/functions_backup.php");

		if ($_POST["createtable"] == "1")
		{
			$createtable = true;
		} else {
			$createtable = false;
		}

		if ($_POST["compress"] == "1")
		{
			$docompress = true;
		} else {
			$docompress = false;
		}

		doBackup($createtable, $docompress);
	} else {
		require_once ("./includes/functions_html.php");

		$interface->adminHeader($_SWIFT["language"]["database"]." > ".$_SWIFT["language"]["backup"], 1);

		$interface->adminNavBar($_SWIFT["language"]["backup"], $_SWIFT["language"]["desc_backup"], 11);

		printFormStart();
		printMainTableHeader($_SWIFT["language"]["backupoptions"]);

		printYesNoRow("createtable", $_SWIFT["language"]["title_createtable"], $_SWIFT["language"]["desc_createtable"], true);
		printYesNoRow("compress", $_SWIFT["language"]["title_compressdump"], $_SWIFT["language"]["desc_compressdump"], true);

		printSubmitRow($_SWIFT["language"]["genbackup"]);

		printMainTableFooter();

		addHiddenField("_m", "core");
		addHiddenField("_a", "backup");
		addHiddenField("step", "1");
		printFormEnd();

		$interface->adminFooter();
	}


/**
* ###############################################
* Repair/Optimize Database
* ###############################################
*/
} else if ($eventaction == "repair") {
	require_once ("./includes/functions_html.php");

	/**
	* Custom Actions
	*/
	if ($_GET["do"] == "repair" && trim($_GET["table"]) != "")
	{
		$dbCore->query("REPAIR TABLE `". $dbCore->escape($_GET["table"]) ."`;");
		$result = $dbCore->nextRecord();

		$infomessage = sprintf($_SWIFT["language"]["repairedtable"], $_GET["table"], $result["Msg_text"]);
	} else if ($_GET["do"] == "optimize") {
		$dbCore->query("OPTIMIZE TABLE `". $dbCore->escape($_GET["table"]) ."`;");
		$result = $dbCore->nextRecord();

		$infomessage = sprintf($_SWIFT["language"]["optimizedtable"], $_GET["table"], $result["Msg_text"]);
	} else if ($_GET["do"] == "repairall") {
		$tablelist = getTableList();
		$tablecount = 0;
		foreach ($tablelist as $key=>$val)
		{
			$tablecount++;
			$dbCore->query("REPAIR TABLE `". $dbCore->escape($val) ."`;");
		}
		
		$infomessage = sprintf($_SWIFT["language"]["repairedalltables"], $tablecount);
	} else if ($_GET["do"] == "optimizeall") {
		$tablelist = getTableList();
		$tablecount = 0;
		foreach ($tablelist as $key=>$val)
		{
			$tablecount++;
			$dbCore->query("OPTIMIZE TABLE `". $dbCore->escape($val) ."`;");
		}
		
		$infomessage = sprintf($_SWIFT["language"]["optimizedalltables"], $tablecount);
	}


	$interface->adminHeader($_SWIFT["language"]["database"]." > ".$_SWIFT["language"]["repairoptimize"]);

	$interface->adminNavBar($_SWIFT["language"]["repairoptimize"], $_SWIFT["language"]["desc_repairoptimize"], 11);

	printInfoBox($infomessage);

	printMainTableHeader($_SWIFT["language"]["tablelist"]);

	$colarray[0]["align"] = "left";
	$colarray[0]["nowrap"] = true;
	$colarray[0]["value"] = $_SWIFT["language"]["tablename"];
	$colarray[1]["width"] = "100";
	$colarray[1]["value"] = $_SWIFT["language"]["tabletype"];
	$colarray[2]["width"] = "100";
	$colarray[2]["value"] = $_SWIFT["language"]["rows"];
	$colarray[3]["width"] = "150";
	$colarray[3]["value"] = $_SWIFT["language"]["size"];
	$colarray[4]["width"] = "150";
	$colarray[4]["value"] = $_SWIFT["language"]["indexsize"];
	$colarray[5]["width"] = "200";
	$colarray[5]["value"] = $_SWIFT["language"]["options"];
	
	printDataRow($colarray, "tabletitlerow");


	$dbCore->query("SHOW TABLE STATUS FROM `". $dbCore->escape($_DB["name"]) ."`;");
	while ($dbCore->nextRecord())
	{
		$colarray = array();
		$colarray[0]["align"] = "left";
		$colarray[0]["value"] = $dbCore->Record["Name"];

		if (strtoupper($dbCore->Record["Type"]) == "HEAP")
		{
			$colarray[1]["value"] = "<span class='texthighlightred'>".$dbCore->Record["Type"]."</span>";
		} else {
			$colarray[1]["value"] = "<span class='texthighlightblue'>".$dbCore->Record["Type"]."</span>";
		}

		$colarray[2]["value"] = number_format($dbCore->Record["Rows"], 0, "", $_SWIFT["language"]["thousandsseperator"]);
		$colarray[3]["value"] = formattedSize($dbCore->Record["Data_length"]);
		$colarray[4]["value"] = formattedSize($dbCore->Record["Index_length"]);

		$colarray[5]["value"] = '<a href="index.php?_m=core&_a=repair&do=repair&table='. urlencode($dbCore->Record["Name"]) .'" title="'. $_SWIFT["language"]["repair"] .'"><img src="'. $_SWIFT["themepath"] .'icon_repair.gif" border="0">&nbsp;'.$_SWIFT["language"]["repair"].'</a>&nbsp;&nbsp;<a href="index.php?_m=core&_a=repair&do=optimize&table='. urlencode($dbCore->Record["Name"]) .'" title="'. $_SWIFT["language"]["optimize"] .'"><img src="'. $_SWIFT["themepath"] .'icon_optimize.gif" border="0">&nbsp;'.$_SWIFT["language"]["optimize"].'</a>';

		printDataRow($colarray);
	}

	$button[0]["value"] = $_SWIFT["language"]["repairall"];
	$button[0]["link"] = "index.php?_m=core&_a=repair&do=repairall";
	$button[1]["value"] = $_SWIFT["language"]["optimizeall"];
	$button[1]["link"] = "index.php?_m=core&_a=repair&do=optimizeall";
	printButtonRow($button, count($colarray));


	printMainTableFooter();

	$interface->adminFooter();
}


?>